

class Easy_Link_Creator

  attr_accessor :controller_matrix

  def initialize ( x , y )

    @column_length = x
    @row_length = y

    @controller_matrix = Array.new()

    create_matrix()

  end

  def create_matrix

    # declare temporary buffer
    controller_list = Array.new()

    # create top row of boundary controllers
    for controller in (0..(@column_length + 1))
      controller_list.push("b#{ controller }")
    end

    # add top row
    @controller_matrix.push( Array.new( controller_list ) )

    #clear the list to continue
    controller_list.clear

    # fill in controller rows
    for row in (1..@row_length)

      # create west boundary controller
      controller_list.push( "b#{ @column_length + ( row * 2 ) }" )

      # create active controller
      for column in (1..@column_length)
        controller_list.push( "#{ column + ( ( row - 1 ) * @column_length ) }" )
      end

      # create east boundary controller
      controller_list.push( "b#{ @column_length + ( row * 2 ) + 1 }" )

      # add current row
      @controller_matrix.push( Array.new( controller_list ) )

      #clear the list to continue
      controller_list.clear
      
    end

    # create bottom row of boundary controllers
    for column in (0..(@column_length + 1))
      controller_list.push( "b#{ column + (@column_length+((@row_length+1)*2)) }" )
    end

    # add current row
    @controller_matrix.push( Array.new( controller_list ) )

    #clear the list to continue
    controller_list.clear
    
  end

  def getControllerLink( controller, direction_symbol )

    # find row and column of controller
    for controller_array in @controller_matrix
      row = @controller_matrix.index( controller_array )
      if ( nil != ( column = controller_array.index( controller.to_s() ) ) )
        found = true
        break
      end
    end

    if ( found == true )

      case direction_symbol
      when :N
        if ( row > 0 )
          return @controller_matrix[row - 1][column]
        end
      when :NE
        if ( ( row > 0 ) and ( column < ( @column_length + 1 ) ) )
          return @controller_matrix[row - 1][column + 1]
        end
      when :NW
        if ( ( row > 0 ) and ( column > 0 ) )
          return @controller_matrix[row - 1][column - 1]
        end
      when :S
        if ( row < ( @row_length + 1 ) )
            return @controller_matrix[row + 1][column]
        end
      when :SE
        if ( ( row < ( @row_length + 1 ) ) and ( column < ( @column_length + 1 ) ) )
            return @controller_matrix[row + 1][column + 1]
        end
      when :SW
        if ( ( row < ( @row_length + 1 ) ) and ( column > 0 ) )
            return @controller_matrix[row + 1][column - 1]
        end
      when :E
        if ( column < ( @column_length + 1 ) )
          return @controller_matrix[row][column + 1]
        end
      when :W
        if ( column > 0 )
          return @controller_matrix[row][column - 1]
        end
      else
        puts "WRONG SYMBOL FOR GET LINK"
      end
    else
      puts "WRONG PROCESSOR SEARCHED"
    end

  end

  def get_max_controller
    return ( @row_length * @column_length )
  end

  def get_max_boundary_controller
    return ( ( ( @row_length + 1 ) * 2 ) + ( @column_length * 2 ) + 1 )
  end

end
